﻿using FineUI;
using SysManage.BLL;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models;
using ZHDJ.WebPage;

namespace SysManage.Module.Org
{
    public partial class PartyOrgLeaderWindowEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            if (!IsSystemAdmin)
            {
                ppBranch.Enabled = false;
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyOrgLeaderWindow leader = DB.PartyOrgLeaderWindows.Find(id);
                if (leader == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtUsweName.Text = leader.UserName;
                txtPost.Text = leader.Post;
                txtduty.Text = leader.duty;
                txtOrderNo.Text = Convert.ToString(leader.OrderNo);
                ppBranch.Value = leader.BranchID;
                ppBranch.Text = ControlBusiness.GetBranchPickerText(leader.BranchID);
                heIntroduction.Text = leader.Introduction;
                WorkDistribution.Text = leader.WorkDistribution;

                filePhoto.Text = leader.ImageUrl;

                hdImageId.Text = leader.ImageUrl;
                if (!string.IsNullOrEmpty(hdImageId.Text))
                {
                    filePhoto.EmptyText = UpFileBusiness.GetFileName(hdImageId.Text);
                    HyperLink1.ImageID = hdImageId.Text;
                }
            }
            else
            {
                ppBranch.Value = this.BranchID;
                ppBranch.Text = ControlBusiness.GetBranchPickerText(this.BranchID);
            }
           
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            PartyOrgLeaderWindow leader;

            if (id != "")
            {
                //更新
                leader = DB.PartyOrgLeaderWindows.Find(id);
                if (leader == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                DB.SaveChanges();
            }
            else
            {
                leader = new PartyOrgLeaderWindow();
                leader.ID = StringHelper.NewID;
                leader.BranchID = ppBranch.Value;
                leader.CreateTime = DateTime.Now;
                leader.CreateBy = CurrentUser.LoginID;
                leader.Introduction = heIntroduction.Text;

                DB.PartyOrgLeaderWindows.Add(leader);
            }

            leader.UserName = txtUsweName.Text;
            leader.Post = txtPost.Text.Trim();
            leader.duty = txtduty.Text;
            leader.OrderNo = Convert.ToInt32(txtOrderNo.Text.Trim());
            leader.ModifyTime = DateTime.Now;
            leader.ModifyBy = CurrentUser.LoginID;
            leader.Introduction = heIntroduction.Text;
            leader.WorkDistribution = WorkDistribution.Text;
            leader.Category = Hz2Py.Convert(txtUsweName.Text);
            if (!string.IsNullOrEmpty(hdImageId.Text))
            {
                leader.ImageUrl = hdImageId.Text;
            }

            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion


        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdImageId.Text = rs.FileID;
                HyperLink1.ImageID = rs.FileID;
            }
        }
    }
}